<template>
  <div class="login">
    <el-form :model="loginForm" class="form" :rules="rules" ref="loginForm">
      <p>后台管理系统</p>
      <el-form-item prop="userName">
        <el-input
          v-model="loginForm.userName"
          placeholder="请输入用户名"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          placeholder="请输入密码"
          show-password
        ></el-input>
      </el-form-item>
      <el-button type="primary" block @click="submit" :loading="loading"
        >登录</el-button
      >
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loginForm: {},
      loading: false,
      rules: {
        userName: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    submit() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$http.post('/api/token').then(res => {
            this.$cookie.set('token', res.data.token)
            this.$router.replace({ path: this.$route.query.redirect || '/' })
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.login {
  height: 100vh;
  // background: url('../../assets/logo.png') no-repeat center center;
  .form {
    width: 300px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    p {
      font-size: 30px;
    }
    button {
      width: 100%;
    }
  }
}
</style>
